﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Filter;
    using Helper;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>Filter specified properties form.</summary>
    public partial class ColoredCannyForm : Form, IOptionsForm
    {
        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        private readonly CannyEdgeDetector canny;
        private bool drawInImage;        
        private readonly SimpleDilatation dilatation;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColoredCannyForm"/> class.
        /// </summary>
        /// <param name="processor">The SBIP processor.</param>
        public ColoredCannyForm(Processor processor)
        {
            InitializeComponent();
            canny = AOF.Init(processor, this, gbProcess, 
                                new CannyEdgeDetector()) as CannyEdgeDetector;
            dilatation = new SimpleDilatation();
        }

        /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            Bitmap b;
            if (drawInImage)
            {
                CannyEdgeMarker marker = new CannyEdgeMarker
                {
                    CannyEdgeDetector = canny,
                    SimpleDilatation = dilatation
                };
                b = marker.Apply(Processor.OriginalImage);
            }
            else
            {
                if (rbOrientColored.Checked)
                {
                    b = canny.AdditionalApply(Processor.OriginalImage);
                    if (dilatation.Diameter > 1)
                    {
                        b = dilatation.Apply(b);
                        ColorPalette.SetCannyColorPalette(b);
                    }
                }
                else
                {
                    b = canny.Apply(Processor.OriginalImage);
                    if (dilatation.Diameter > 1)
                    {
                        b = dilatation.Apply(b);
                    }
                }

            }

            string s = Program.L.Text[40] + Program.L.Text[8] + ": High=" + 
                canny.HighThreshold + "; Low=" + canny.LowThreshold +
                "; Size=" + canny.GaussianSize + "; Sigma=" + canny.GaussianSigma + "; "
                + Program.L.Text[27] + "=" + 
                (rbOrientColored.Checked ? Program.L.Text[38] : Program.L.Text[28]) +
                "; " + Program.L.Text[33] + "=" + 
                (rdBtnNoDrawInImage.Checked ? Program.L.Text[34] : Program.L.Text[35]) +
                "; " + Program.L.Text[37] + "=" + lbDiameter.Text;
            Processor.Change(b, s);            
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[8];
            tabctrlOptions.TabPages[1].Text = Program.L.Text[26];
            gbDrawModes.Text = Program.L.Text[27];
            rbNormal.Text = Program.L.Text[28];            
            gbDrawInImage.Text = Program.L.Text[33];
            rdBtnNoDrawInImage.Text = Program.L.Text[34];
            rdBtnYesDrawInImage.Text = Program.L.Text[35];
            gbLineThickness.Text = Program.L.Text[36];
            lbDiameterText.Text = Program.L.Text[37];
            rbOrientColored.Text = Program.L.Text[38];
            tabctrlOptions.TabPages[0].Text = Program.L.Text[39];
        }

        #region Canny tab control events

        private void TrbarHighThresholdValueChanged(object sender, EventArgs e)
        {
            canny.HighThreshold = (byte)trbarHighThreshold.Value;
            lbHighThreshold.Text = trbarHighThreshold.Value.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrbarLowThresholdValueChanged(object sender, EventArgs e)
        {
            canny.LowThreshold = (byte)trbarLowThreshold.Value;
            lbLowThreshold.Text = trbarLowThreshold.Value.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrbarGaussSizeValueChanged(object sender, EventArgs e)
        {
            canny.GaussianSize = trbarGaussSize.Value;
            lbGaussSize.Text = trbarGaussSize.Value.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrbarGaussSigmaValueChanged(object sender, EventArgs e)
        {
            canny.GaussianSigma = trbarGaussSigma.Value / 1000.0f;
            lbGaussSigma.Text = (trbarGaussSigma.Value / 1000.0f).ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }
        #endregion Canny tab control events

        #region Edge Drawing tab control events
        private void RbOrientColoredCheckedChanged(object sender, EventArgs e)
        {
            pboxSemiCircle.Visible = ((RadioButton)sender).Checked;
            if (rbOrientColored.Checked)
            {
                rdBtnNoDrawInImage.Checked = true;
            }
            Processor.ApplyFilterRTP();
        }

        private void RdBtnYesDrawInImageCheckedChanged(object sender, EventArgs e)
        {
            drawInImage = rdBtnYesDrawInImage.Checked;
            if (rdBtnYesDrawInImage.Checked)
            {
                rbNormal.Checked = true;
            }
            Processor.ApplyFilterRTP();
        }

        private void TrBarDiameterValueChanged(object sender, EventArgs e)
        {
            dilatation.Diameter = (byte)(trBarDiameter.Value);// * 2 + 1);
            lbDiameter.Text = dilatation.Diameter.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        #endregion Edge Drawing tab control events

        private void BtnProcessClick(object sender, EventArgs e)
        {
            try
            {
                ApplyFilter();   
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }
    }
}
